package com.snow.useviewdemo.glide;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.snow.img.ImageUtil;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.adapter.NAdapter;
import com.snow.useviewdemo.base.BaseActivity;
import com.snow.useviewdemo.bean.ItemBean;
import com.snow.useviewdemo.bean.MainListBean;
import com.snow.useviewdemo.databinding.ActivityImageviewShowBinding;
import com.snow.useviewdemo.util.TextViewUtils;

import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaoyingtao
 * Date: 2020/6/21
 * Describe:
 */
public class ImageViewShowActivity extends BaseActivity<ActivityImageviewShowBinding> {
    //矮图
    private String imgUrl = "http://img3.imgtn.bdimg.com/it/u=2093357363,3958264359&fm=214&gp=0.jpg";
    //长方形图
//    private String imgUrl = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3252521864,872614242&fm=26&gp=0.jpg";
    //正方形图
//    private String imgUrl = "http://b-ssl.duitang.com/uploads/item/201804/30/20180430215916_zrwau.jpg";
    //更长图
    //    private String imgUrl = "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1552403063,4008993671&fm=26&gp=0.jpg";

    //获取itemlayout  1 验证scaleType对minHeight和maxHeight的影响======== 默认查看scaleType的效果
    private int itemType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_imageview_show;
    }

    @Override
    protected void setViewData(Bundle savedInstanceState) {
        itemType = 1;
        verScaleType();
        /**
         * 验证结果：
         * 1、图片设置固定宽，高自适应wrap_content，最大最小高度只能设置其一，不能同时设置，否则会有一个无效
         * 最小高度需要动态设置，xml中配置无效，setMinimumHeight（），不能设置setAdjustViewBounds(true)，否则失效
         * 最大高度xml和动态都可以
         * 动态设置：
         *  mBinding.imageView09.setAdjustViewBounds(true);
         *  mBinding.imageView09.setMaxHeight(TextViewUtils.init().num2Dip(this,300));
         *  xml设置：
         * android:adjustViewBounds="true"
         * android:maxHeight="@dimen/margin_150"
         * 2、scaleType属性
         * android:adjustViewBounds="true"
         * android:maxHeight="@dimen/margin_600"
         * android:minHeight="@dimen/margin_200"
         * android:scaleType="xxx属性值"
         * centerCrop：只会显示minHeight高度的值，图即使高于200依然只显示200的高度，言外之意maxHeight失效
         * fit
         */
    }

    private void verScaleType() {
//        String imgUrl = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3252521864,872614242&fm=26&gp=0.jpg";
        String imgUrl = "http://b-ssl.duitang.com/uploads/item/201804/30/20180430215916_zrwau.jpg";
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));

        NAdapter nAdapter = new NAdapter<ItemBean>(this, getItemLayout(), null) {
            @Override
            public void onBindViewHolder(NViewHolder holder, ItemBean bean, int position) {
                TextView tv_expend = holder.itemView.findViewById(R.id.tv_expend);
                ImageView imageView = holder.itemView.findViewById(R.id.imageView);
                eeeee(tv_expend, imageView, bean);
            }
        };
        mBinding.recyclerView.setAdapter(nAdapter);
        List<ItemBean> itemList = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            itemList.add(new ItemBean(i, imgUrl));
        }
        nAdapter.replaceData(itemList);
    }

    /**
     * 获取不同的itemView
     * @return
     */
    private int getItemLayout() {
        if (itemType == 1) {
            return R.layout.adapter_image_test_item2;
        }
        return R.layout.adapter_image_test_item;
    }

    private void eeeee(TextView tv_expend, ImageView imageView, ItemBean itemBean) {
        switch (itemBean.getType()) {
            case 0:
                tv_expend.setText("未设置scaleType");
                break;
            case 1:
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                tv_expend.setText("scaleType=\"centerCrop\"");
                break;
            case 2:
                imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                tv_expend.setText("scaleType=\"centerInside\"");
                break;
            case 3:
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                tv_expend.setText("scaleType=\"fitCenter\"");
                break;
            case 4:
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                tv_expend.setText("scaleType=\"fitXY\"");
                break;
            case 5:
                imageView.setScaleType(ImageView.ScaleType.FIT_START);
                tv_expend.setText("scaleType=\"fitStart\"");
                break;
            case 6:
                imageView.setScaleType(ImageView.ScaleType.FIT_END);
                tv_expend.setText("scaleType=\"fitEnd\"");
                break;
            case 7:
                imageView.setScaleType(ImageView.ScaleType.MATRIX);
                tv_expend.setText("scaleType=\"matrix\"");
                break;
            case 8:
                itemBean.setUrl(GetScreenshotImgUtils.getLatestPhoto(this));
                tv_expend.setText("最新的拍照或者截图图片");
                break;
        }
        imageLoad(itemBean.getUrl(), imageView);
    }

    private void setImgData() {
        //        ImageUtil.imageLoad(this, imgUrl, mBinding.imageView01);
//        ImageUtil.imageLoad(this, imgUrl, mBinding.imageView02);
//        ImageUtil.imageLoad(this, imgUrl, mBinding.imageView03);
//        ImageUtil.imageLoad(this, imgUrl, mBinding.imageView04);
        imageLoad("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3252521864,872614242&fm=26&gp=0.jpg", mBinding.imageView15);
        imageLoad(imgUrl, mBinding.imageView16);
        //矮图
//        imageLoad("http://img3.imgtn.bdimg.com/it/u=2093357363,3958264359&fm=214&gp=0.jpg", mBinding.imageView17);
        //长图
//        imageLoad("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2546481120,2146947868&fm=26&gp=0.jpg", mBinding.imageView17);
        //更长图
        imageLoad("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1552403063,4008993671&fm=26&gp=0.jpg", mBinding.imageView17);

        imageLoad(imgUrl, mBinding.imageView01);
        imageLoad(imgUrl, mBinding.imageView02);
        imageLoad(imgUrl, mBinding.imageView03);
        imageLoad(imgUrl, mBinding.imageView04);
        imageLoad(imgUrl, mBinding.imageView05);
        imageLoad(imgUrl, mBinding.imageView06);
        imageLoad(imgUrl, mBinding.imageView07);
        imageLoad(imgUrl, mBinding.imageView08);
        imageLoad(imgUrl, mBinding.imageView09);
        imageLoad(imgUrl, mBinding.imageView010);
        imageLoad(imgUrl, mBinding.imageView011);
        //设置最小高度不能设置此属性。否则无效
//        mBinding.imageView03.setAdjustViewBounds(true);
        mBinding.imageView03.setMinimumHeight(TextViewUtils.init().num2Dip(this, 300));
        mBinding.imageView09.setAdjustViewBounds(true);
        mBinding.imageView09.setMaxHeight(TextViewUtils.init().num2Dip(this, 300));
        new CountDownTimer(3000, 3000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
//                logImageWidthHeight(mBinding.imageView01);
//                logImageWidthHeight(mBinding.imageView02);
//                logImageWidthHeight(mBinding.imageView03);
//                logImageWidthHeight(mBinding.imageView04);
//                logImageWidthHeight(mBinding.imageView05);
//                logImageWidthHeight(mBinding.imageView06);
//                logImageWidthHeight(mBinding.imageView07);
                logImageWidthHeight(mBinding.imageView15);
                logImageWidthHeight(mBinding.imageView16);
                logImageWidthHeight(mBinding.imageView17);
//                if (mBinding.imageView17.getHeight()<400){
//                    ViewGroup.LayoutParams layoutParams = mBinding.imageView17.getLayoutParams();
//                    layoutParams.height = 500;
//                    mBinding.imageView17.setLayoutParams(layoutParams);
//                }
            }
        }.start();
    }

    private void imageLoad(String imgUrl, ImageView imageView) {
        Glide.with(this).load(imgUrl).into(imageView);
    }

    private void logImageWidthHeight(ImageView imageView) {
        int width = imageView.getWidth();
        int height = imageView.getHeight();
        Log.e("imageWidthHeight", "===imgId====" + imageView.getId() + "====width===" + width + "====height===" + height);
    }
}
